package com.carryGo.service.hzl;

import com.carryGo.dao.hzl.BaseDao;
import com.carryGo.entity.Commodity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName:IndexService
 * package:com.carryGo.service.hzl
 * Description:
 *
 * @Date:2020/8/27 13:28
 * @Author: tank_黄志凌
 */
public class IndexService extends BaseDao {

    public List<Commodity> queryObjs(String sql,Object...params) {
        Connection conn = getConnection();
        PreparedStatement pst = null;
        ResultSet rs = null;
        List<Commodity> commodities = new ArrayList<>();
        try {
            pst = conn.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                pst.setObject(i + 1, params[i]);
            }
            rs = pst.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("id");
                String commName = rs.getString("commName");
                double price = rs.getDouble("price");
                String unit = rs.getString("unit");
                String type = rs.getString("type");
                String commInform = rs.getString("commInform");
                String pic = rs.getString("pic");
                String businessName = rs.getString("businessName");
                commodities.add(new Commodity(id, commName, price, unit, type, commInform, pic, businessName));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return commodities;
    }


        /**
     * 搜索功能
     */
    public List<Commodity> queryComm(String commName){
        String sql="select * from commodity where commName like ?";
        List<Commodity> commodities=new ArrayList<>();
        commodities=queryObjs(sql,"%"+commName+"%");
        return commodities;
    }





}




